/**
 * @class   wosh::devices::ModemDslBundle
 * @brief   Abstraction of a DSL Modem for POSIX systems.
 *          Designed for \c PPPoE driver (\c pppstats).
 *
 * Works as Bundle and register itself as Device.
 * Offers live (auto-refreshed) properties (stats).
 *
 * Real implementation is class wosh::devices::ModemDsl, most
 * are just a proxy to that internal object.
 *
 * @see     wosh::interfaces::devices::ModemDsl
 * @ingroup Devices
 ****************************************************************************
 * @version 0.8.467 $Id: ModemDslBundle.h 2866 2010-08-07 15:01:48Z alex $
 * @author  Alessandro Polo
 * @date    Released on 2009-06-04
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Bundles_Devices_ModemDsl_H__
 #define __WOSH_Bundles_Devices_ModemDsl_H__

 #include <core/BundleGeneric.h>
 #include <core/Thread.h>

 #include <interfaces/Device.h>
 #include <interfaces/devices/Modem.h>
 #include <interfaces/devices/ModemDsl.h>
 #include <interfaces/events/ModemEvents.h>

 #include "IModemDslListener.h"

class ModemDslImpl;

namespace wosh {
 namespace devices {

 #define _ModemDsl_NAME							"ModemDsl"
 #define _ModemDsl_VERSION						1.01

 #define _ModemDsl_DEFAULT_pppInterface			"ppp0"
 #define _ModemDsl_DEFAULT_PingCheckAddress		"google.com"

 #define _ModemDsl_DEFAULT_Script				"$CONFIG/scripts/dslmodem.sh"


class ModemDslBundle :	public BundleGeneric,
						public virtual wosh::interfaces::Device,
						public virtual wosh::interfaces::devices::Modem,
						public virtual wosh::interfaces::devices::ModemDsl,
						public virtual IModemDslListener
 {
	WOSH_BUNDLE(wosh::devices::ModemDslBundle)
	WOSH_BUNDLE_INTERFACE("wosh::interfaces::devices::ModemDsl")
	WOSH_BUNDLE_SINGLETON(0,0)

	public:
		/**
		 * @brief   Empty constructor, init bundle and vars, Properties and Methods.
		 * @param name [in] optional name of the bundle   
		 */
		ModemDslBundle( const std::string& name = "" );
		/**
		 * @brief   Empty destructor, may (synch) stop bundle and internal threads when still running
		 */
		virtual ~ModemDslBundle();


	public:
		WRESULT bundleStart();
		WRESULT bundleStop();
		WRESULT bundleDiagnostic();

		bool updatingProperty( bool& do_update, const Variant& value_proposed, Property& property_current, const PropertiesProvider* source );
		void busMessage( const Message& message, const Bus* source );

//@}
/** @name Device Interface
 * @{
 ******************************************************************************/
	public:
		wosh::interfaces::Device::DEVICE_STATE getDeviceState() const { return this->devState; }
//@}
/** @name ModemDsl Interface
 * @{
 ******************************************************************************/
	public:
		wosh::interfaces::devices::Modem::CONNECTION_STATE getConnectionState() const { return this->connState; }

		/**
		 * @brief   Connect modem. Raise Fact when connected.
		 * @param params [in] Connection parameters (when required)
		 * @return  WRET_ERR_INTERNAL if something went wrong; WRET_OK is modem has been disconnected.
		 */
		WRESULT connect( std::string params = "" );
		/**
		 * @brief   Disconnect modem. Raise Fact when disconnected.
		 * @return  WRET_ERR_INTERNAL if something went wrong; WRET_OK is modem has been disconnected.
		 */
		WRESULT disconnect();
		/**
		 * @brief   Reconnect modem. Raise Fact when connected.
		 * @return  WRET_ERR_INTERNAL if something went wrong; WRET_OK is modem has been disconnected.
		 */
		WRESULT reconnect();
		/**
		 * @brief   Force connection check. Raise Fact when connection state changed.
		 * @return  WRET_ERR_INTERNAL if something went wrong; WRET_OK if check was successful.
		 */
		WRESULT checkConnected();
		/**
		 * @brief   Force stats reset.
		 * @return  WRET_NOT_IMPLEMENTED or WRET_ERR_ILLEGAL_USE if feature is not available;
		 *          WRET_ERR_INTERNAL if something went wrong; WRET_OK if check was successful.
		 */
		WRESULT resetStats();

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief   Get period of refresh loop
		 * @return  refresh period in seconds
		 */
		long getRefreshFrequency() const;
		/**
		 * @brief   Get address used while checking connection through ping
		 * @return  IPv4 or hostname address (example: www.google.com)
		 */
		std::string getPingCheckAddress() const;
		/**
		 * @brief   Get full path of support script, used to control the modem
		 * @return  full path of script (example: bin/scripts/dslmodem.sh)
		 */
		std::string getCtrlScript() const;
		/**
		 * @brief   Get POSIX interface associated with connection
		 * @return  interface name (example: ppp0)
		 */
		std::string getPppInterface() const;

//@}
/** @name Modem Events
 * @{
 ******************************************************************************/
 	public:
		void modem_ConnectionState( ModemDslImpl* modem, wosh::interfaces::devices::Modem::CONNECTION_STATE state );
		void modem_StatsUpdate( ModemDslImpl* modem, const pppStats& stats );
//@}
/** @name Thread Events
 * @{
 ******************************************************************************/
 	public:
		void thread_event( Thread::THREAD_STATE thread_event, Thread* thread_source );
//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:
		/**
		 * @brief   Update Connection State of Modem, internal var and Property. Also may raise Device event
		 * @param state [in] the new connection's state
		 * @param raiseEvent [in] pick if/how raise Device Event. '0' won't raise event; '2' will always raise event;
		 *                         '1' will raise event only if connection's state changed. Event message (Fact) contains
		 *                         CONNECTION_STATE as string.
		 */
		void setConnectionState( wosh::interfaces::devices::Modem::CONNECTION_STATE state, int raiseEvent = 1 );
//@}
	private:
		/**
		 * @brief   Connect method, accept connection parameters as (first) argument
		 * @param request [in] the Request representation
		 * @return  standard WRESULT return value and connection state as response's data
		 */
		Response* mmDoConnect( const Request* request );
		/**
		 * @brief   Force-State method, forces current Modem-Connection state to passed value (CONNECTION_STATE as string)
		 * @param request [in] the Request representation
		 * @return  standard WRESULT return value and connection state as response's data
		 */
		Response* mmDoForceState( const Request* request );
		/**
		 * @brief   Check-Connected method, force connection-check (using script)
		 * @param request [in] the Request representation
		 * @return  standard WRESULT return value and connection state as response's data
		 * @note    method calls directly the bash support script, by default it pings an address
		 * @see     _ModemDsl_KEY_PingCheckAddress
		 */
		Response* mmDoCheckConnected( const Request* request );

	private:
		BusConnector BusDevices;					///< register myself & work as Device too
		ModemDslImpl* modemWorker;					///< real implementation
		wosh::interfaces::devices::Modem::CONNECTION_STATE connState;	///< current state of the modem
		wosh::interfaces::Device::DEVICE_STATE devState;		///< current state of device


}; // class def

 }; // namespace devices
}; // namespace wosh

#endif //__WOSH_Bundles_Devices_ModemDsl_H__
